package cn.itsource.exam.controller;

import cn.itsource.exam.domain.ClassInfo;
import cn.itsource.exam.domain.Login;
import cn.itsource.exam.domain.StudentInfo;
import cn.itsource.exam.dtos.outdto.SubjectClassOutDTO;
import cn.itsource.exam.query.ClassInfoQuery;
import cn.itsource.exam.service.ClassInfoService;
import cn.itsource.exam.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by su on 2020/7/8.
 */
@RestController
@RequestMapping("/class")
public class ClassInfoController {

    @Autowired
    private ClassInfoService classInfoService;


    /**
     * 增加或修改班级
     * @param paramIn
     */
    @RequestMapping(value = "modify",method = RequestMethod.POST)
    public  String modifyClass(@RequestBody ClassInfo paramIn, HttpServletRequest rq){
        Login info = (Login)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return classInfoService.modifyClass(paramIn, info.getName());
    }

    /**
     * 刪除班级
     * @param id
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    public String delClass(Long id, HttpServletRequest rq){
        StudentInfo info = (StudentInfo)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return classInfoService.delClass(id, info.getName());
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "batchdel",method = RequestMethod.POST)
    public String batchDelUser(String ids, HttpServletRequest rq){
        StudentInfo info = (StudentInfo)rq.getSession().getAttribute(rq.getHeader("U-TOKEN"));
        return classInfoService.batchDelUser(ids, info.getName());
    }

    /**
     * 根据主键获取班级
     * @param id
     * @return
     */
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public ClassInfo getClassById(Long id){
        return classInfoService.getClassById(id);
    }

    /**
     * 分页查询班级
     * @param paramIn
     * @return
     */
    @PostMapping("listpage")
    public PageList<ClassInfo> getClassListPage(@RequestBody ClassInfoQuery paramIn){
        return classInfoService.getClassListPage(paramIn);
    }

    @PostMapping("classList")
    public List<ClassInfo> getClassList(Integer type){
        return classInfoService.getClassList(type);
    }


    @PostMapping("subject/tree")
    public List<SubjectClassOutDTO> getSubjectClassTree(){
        return classInfoService.getSubjectClassTree();
    }
}
